const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const {HotModuleReplacementPlugin,ids:{NamedModuleIdsPlugin}} =require('webpack')
const conf={
  mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
  devtool: 'inline-source-map',


  resolve: {
    extensions: [ '.tsx', '.ts', '.js' ]
  },
  optimization:{
    minimize:true,
  },
  module:{
    rules:[
      {
        test:/\.css$/,
        use:[
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  }
}
let config = {}
if(process.env.NODE_ENV==='development'){
  config=Object.assign(conf,{
    entry: './example/src/index.ts',
    output:{
      filename:'index.js',
      path: path.resolve(__dirname, 'dist'),
      libraryTarget:'umd',
      library:'db-reader'
    },
    plugins:[
      new HtmlWebpackPlugin({
        title:'db-reader'
      }),
      new CleanWebpackPlugin(),
      new NamedModuleIdsPlugin(),
      new HotModuleReplacementPlugin()
    ],
    devServer:{
      static:'./dist',
      hot:true
    },
  })
}else{
  config =Object.assign(conf,{
    entry: './packages/index.ts',
    output:{
      filename:'index.js',
      path:path.resolve(__dirname,'build'),
      library:'db-reader',
      libraryTarget:'umd'
    },
    plugins: [
      new CleanWebpackPlugin(),    // 编译之前清空 /dist
    ],
  })
}
module.exports=config
